package com.terren.excavator.content;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.ansj.util.FilterModifWord;

/**
 * 
 * @author Administrator
 *
 */
public class ContentManager {
	
	/**
	 * 通过 url 获得正文
	 * @param url
	 * @return
	 */
	public static String getContentFromUrl(String url){
		String result = null;
		try {
			ExcavateContent ec = new ExcavateContent(url);
			
			result = ec.getUrlContent();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 将 list 中的数据放到 map 中
	 * @param wordList
	 * @return
	 */
	public static Map<String, Integer> getWordMap(List<Term> wordList){
		Map<String, Integer> wordMap = new HashMap<String, Integer>();//分词   分词出现的个数
		for(Term oneTerm : wordList){
			String oneWord = oneTerm.getName();
			int count = 1;
			if(wordMap.containsKey(oneWord)){
				count = wordMap.get(oneWord) + 1;
			}
			wordMap.put(oneWord, count);
		}
		return wordMap;
	}
	
	/**
	 * 拆分正文的词
	 * @param contentText
	 */
	public static List<SplitWord> splitContentWord(String contentText){
		
		List<Term> wordList = ToAnalysis.parse(contentText) ;
		
		Map<String, Integer> wordMap = getWordMap(wordList);
		
		List<SplitWord> splitList = new LinkedList<SplitWord>();
		
		for(String key : wordMap.keySet()){
			int value = wordMap.get(key);
			SplitWord split = new SplitWord();
			
			split.setWord(key);
			split.setCount(value);
			splitList.add(split);
		}
		
		Collections.sort(splitList, new Comparator<SplitWord>() {
			public int compare(SplitWord o1, SplitWord o2) {
				return (o2.getCount() - o1.getCount());
			}
		});
		return splitList;
	}
	
	/**
	 * 过滤正文分词
	 * @param contentText
	 */
	public static List<SplitWord> filterSplitWord(String contentText){
		
		List<Term> wordList = ToAnalysis.parse(contentText) ;
		
		wordList = FilterModifWord.modifResult(wordList);
		
		Map<String, Integer> wordMap = getWordMap(wordList);
		
		List<SplitWord> splitList = new LinkedList<SplitWord>();
		
		for(String key : wordMap.keySet()){
			int value = wordMap.get(key);
			SplitWord split = new SplitWord();
			
			split.setWord(key);
			split.setCount(value);
			splitList.add(split);
		}
		
		Collections.sort(splitList, new Comparator<SplitWord>() {
			public int compare(SplitWord o1, SplitWord o2) {
				return (o2.getCount() - o1.getCount());
			}
		});
		return splitList;
	}
	
	
	public static void main(String[] args) {
		String url = "http://legal.gmw.cn/2013-10/29/content_9327092.htm";
		
		String contentText = getContentFromUrl(url);
		System.out.println(contentText);
		
		List<SplitWord> list = splitContentWord(contentText);
		
		for(SplitWord word : list){
			System.out.println(word.getWord() + " : " + word.getCount());
		}
		
	}
}
